﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Catering.View_Operaction;
using System.Collections;
using System.Text;
using Microsoft.Office.Core;
using System.IO;
using System.Web.Services;
using PageCommon;
using Catering_Rules;

namespace Catering.Catering_Page.Caipin
{
    public partial class Shipu_Week_Monitor : UserIsNull
    {
        Catering_Rules.OperationArea tOperationArea = new Catering_Rules.OperationArea();   //作业区操作
        Catering_Rules.ClassInfo tClassInfo = new Catering_Rules.ClassInfo();               //班组操作
        T_RecipeInformation tRecipeInformation = new T_RecipeInformation();                 //食谱详细操作

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Txt_Date.Text = DateTime.Now.ToString("yyyy-MM");
                this.Dr_Operaction.DataBingByDt(tOperationArea.GetList("", true).Tables[0], "id", "AreaName", true);
                DataTable ClassDt = new DataTable();
                if (this.Dr_Operaction.SelectedItem != null && this.Dr_Operaction.SelectedValue != "-1")
                {
                    ClassDt=tClassInfo.GetList("AreaID=" + this.Dr_Operaction.SelectedValue).Tables[0];
                }
                this.Dr_Class.DataBingByDt(ClassDt, "id", "CName", true);
            }
        }

        //计算周期
        public int GetDayCount(int y, int M)
        {
            //每月天数数组
            int[] daysMonth = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            daysMonth[1] = (0 == y % 4 && (y % 100 != 0 || y % 400 == 0)) ? 29 : 28;
            return daysMonth[M - 1];//当月 总天数
        }

        //查询
        public void but_Seach_Click(object sender, EventArgs e)
        {
        
        }

        //加载对应班组
        protected void Dr_Operaction_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Dr_Operaction.SelectedItem != null && this.Dr_Operaction.SelectedValue != "-1")
            {
                this.Dr_Class.DataBingByDt(tClassInfo.GetList("AreaID=" + this.Dr_Operaction.SelectedValue).Tables[0], "id", "CName", true);
            }
        }

        public DateTime GetDateTime()
        {
            DateTime dtime = DateTime.MinValue;
            string strDate = this.Txt_Date.Text.Trim();
            if (!string.IsNullOrEmpty(strDate)) DateTime.TryParse(strDate, out dtime);
            return dtime;
        }

        public string GetDayHtml()
        {
            StringBuilder DayThS = new StringBuilder();   //输出的检查状态表
            DateTime dtime = GetDateTime();
            if (dtime != null && dtime > DateTime.MinValue)
            {
                int DayCount= this.GetDayCount(dtime.Year, dtime.Month);
                for (int i = 1; i <= DayCount; i++)
                {
                    DayThS.Append("<th align=\"center\" style=\"width: 25px;\">"+i.ToString()+"</th>");
                }
            }
            return DayThS.ToString();
        }

        //获取食谱表Html
        public string GetCheckList()
        {
            DataTable ClassDt=tClassInfo.GetList("").Tables[0];
            StringBuilder strCheckHtml = new StringBuilder();   //输出的检查状态表
             DateTime dtime = GetDateTime();
            string StartTime=dtime.Year+"-"+dtime.Month+"-01";
            string EndTime=Convert.ToDateTime(StartTime).AddMonths(1).ToShortDateString();
            string strWhere = "";
            if (this.Dr_Class.SelectedValue != "-1")
            {
                strWhere = " ClassID=" + this.Dr_Class.SelectedValue;
            }
            else if (this.Dr_Operaction.SelectedValue != "-1")
            {
                strWhere = " AreaID=" + this.Dr_Operaction.SelectedValue;
            }
            DataTable ExamineDt = tRecipeInformation.GetExamineDtForMonitor(StartTime, EndTime, strWhere);

            //获取要循环的班组
            string ClassIDS = "";
            if (this.Dr_Class.SelectedValue != "-1")
            {
                ClassIDS=this.Dr_Class.SelectedValue;
            }
            else if (this.Dr_Operaction.SelectedValue != "-1")
            {
                DataRow[] RowList = ClassDt.Select("AreaID=" + this.Dr_Operaction.SelectedValue);
                foreach (DataRow eRow in RowList)
                {
                    ClassIDS += eRow["id"] + ",";
                }
            }
            else
            {
                DataRow[] RowList = ClassDt.Select("");
                foreach (DataRow eRow in RowList)
                {
                    ClassIDS += eRow["id"] + ",";
                }
            }
            ClassIDS = ClassIDS.TrimEnd(',');

            //拼接页面
            int DayCount = this.GetDayCount(dtime.Year, dtime.Month);
            int RowIndex = 1;
            foreach (string TempClassID in ClassIDS.Split(','))
            {
                strCheckHtml.Append("<tr><td align=\"center\">" + RowIndex + "</td><td align=\"center\">" + ClassDt.Select("id=" + TempClassID).First()["CName"].ToString() + "</td>");
                for (int i = 1; i <= DayCount; i++)
                {
                    string TempDate = dtime.Year + "-" + dtime.Month + "-" + i;
                    DataRow TempRow = ExamineDt.Select("Date='" + TempDate + "' and ClassID=" + TempClassID).FirstOrDefault();
                    string IsExamineByDate = (TempRow != null) ? TempRow["IsExamineByDate"].ToString() : "0";
                    if (IsExamineByDate == "1")
                    {
                        strCheckHtml.Append("<td align=\"center\"><img src=\"/Images/yes.png\"/></td>");
                    }
                    else
                    {
                        strCheckHtml.Append("<td align=\"center\"></td>");
                    }
                }
                strCheckHtml.Append("</tr>");
                RowIndex += 1;
            }
            return strCheckHtml.ToString();
        }

    }
}